/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package shoppinglist;

import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;

/**
 *
 * @author dvanh
 */
public class Controller {

    GUI myGUI;
    DBConnection DBC;
    private String amount;

    public Controller() {

    }

    public void startUp() {
        DBC = new DBConnection();
        DBC.connect();
        myGUI = new GUI(this);

        fetchGroceries();
    }

    String[] fetchGroceries() {
        ArrayList<String> list = new ArrayList();
        if (DBC == null) {           
        }
        ResultSet rs = DBC.getGroceries();
        try {
            while (rs.next()) {
                list.add(rs.getString(1));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        String[] StringArray = Arrays.copyOf(list.toArray(), list.toArray().length, String[].class);
        return StringArray;
    }

    public void loadShoppingLists() {
        if (myGUI.shopPanel.listDisplay == false) {
            myGUI.shopPanel.listDisplay = true;
            String grocerylists[] = DBC.getShoppingLists();
            myGUI.shopPanel.loadList(grocerylists);

        } else {
            if (myGUI.shopPanel.listbox.isSelectionEmpty() == false) {
                String selectedList = myGUI.shopPanel.listbox.getSelectedValue().toString();
                myGUI.shopPanel.loadList(DBC.getShoppingList(selectedList));
                myGUI.shopPanel.listDisplay = false;
            }
        }

    }

    public void handleEvent(String keyPress) {
        switch (keyPress) {
            case "Add":
                amount = myGUI.buttonPanel.getAmount().getText();
                addGroceryToList();
                break;
            case "Open":
                loadShoppingLists();
                break;
            case "Save":
                saveGroceryList();
                break;
            case "Update":
                amount = myGUI.buttonPanel.getAmount().getText();
                updateGrocryList();
                break;

            default:
                throw new AssertionError();
        }
    }

    public void loadShoppingList(String shoplist) {
        //get a list from database and load into shoppinglist
    }

    public void addGroceryToList() {
        Boolean exists = false;
        String grocerySelect = myGUI.dbList.getListbox().getSelectedValue().toString();

        System.out.println(grocerySelect);
        String[] listElements = myGUI.shopPanel.getList();
        for (int i = 0; i < listElements.length; i++) {

            if (ListPost.getName(listElements[i]).contains(grocerySelect)) {
                exists = true;
            }
        }
        if (exists == false) {
            String groceryRow = amount + "     " + grocerySelect.toString();
            myGUI.shopPanel.addToList(groceryRow);
        }
    }

    public void updateGrocryList() {
        String grocerySelect = myGUI.dbList.getListbox().getSelectedValue().toString();
        String[] listElements = myGUI.shopPanel.getList();
        for (int i = 0; i < listElements.length; i++) {

            if (ListPost.getName(listElements[i]).contains(grocerySelect)) {
                if (!amount.equals("0")) {

                    String groceryRow = amount + "     " + grocerySelect.toString();
                    listElements[i] = groceryRow;

                    myGUI.shopPanel.loadList(listElements);
                } else {
                    listElements[i] = null;

                    myGUI.shopPanel.loadList(listElements);
                }
            }
        }
    }

    public void saveGroceryList() {
        DBC.saveShoppingList(myGUI.shopPanel.getList());
    }
}
